package com.maolong.inteceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 检查用户是否已经登录
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            // 如果未登录，重定向到登录页面
            log.info("用户未登录，重定向到登录页面");
            response.sendRedirect("/login/login.html");
            return false;
        }
        return true;
    }
}
